package me.luger.project.picc.service;

import me.luger.core.dao.cache.redis.springdata.RedisManager;
import me.luger.core.dao.nutz.service.BaseService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import me.luger.project.picc.entity.PiccMeeting;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@Repository
public class PiccMeetingService extends BaseService<PiccMeeting> {
    public PiccMeetingService(Dao dao) {
        super(dao);
    }

    @Autowired
    RedisManager redisManager;

    public PiccMeeting fetchByPid(String pid){
        PiccMeeting piccMeeting = this.fetchOne(Cnd.where("pid","=",pid));
        return piccMeeting;
    }

    /**
     * wx.picchealth.com/custom/pc/lottery.html?type=xxx
      
     xxx = 1 : “PICC人民健康” APP注册抽奖
        xxx = 2: “一卡通”就医服务卡抽奖
     xxx = 3:  “我和保单有个约会”抽奖
     * @param rank
     * @param count
     * @return
     */
    public List<PiccMeeting> doDraw(int rank,int count){
        List<PiccMeeting> piccMeetingList = null;
        Sql sql = null;
        if(hasDoDraw(rank)){
            sql = Sqls.create("select * from piccMeeting where doPrizeStatus = @rank");
            sql.setParam("rank",rank+2);
        }else{
            sql = Sqls.create("select *,count(distinct userName) from piccMeeting where doPrizeStatus = 1 group by userName order by rand() limit @count");
            sql.setParam("count",count);

        }

        piccMeetingList = this.list(sql);
        if(null == piccMeetingList || piccMeetingList.size() == 0){
            return null;
        }

        if(!hasDoDraw(rank)){
            for(PiccMeeting piccMeeting : piccMeetingList){
                piccMeeting.setDoPrizeStatus(rank + 2);
                this.updateIgnoreNull(piccMeeting);
                updateSameNameUser(piccMeeting.getUserName());
            }
            setDoDrawStatus(rank);
        }

        return piccMeetingList;
    }

    public String export2Excel(){
        Map<Integer,String> rankNameMap = new HashedMap();
        rankNameMap.put(0,"特等奖");
        rankNameMap.put(1,"一等奖");
        rankNameMap.put(2,"二等奖");
        rankNameMap.put(3,"三等奖");
        rankNameMap.put(4,"四等奖");
        try{
            Sql sql = Sqls.create("select * from piccMeeting where doPrizeStatus > 1 order by doPrizeStatus asc");
            List<PiccMeeting> piccMeetingList = this.list(sql);
            if(null == piccMeetingList || piccMeetingList.size()==0){
                return "";
            }

            if(hasDoDraw(0)&&hasDoDraw(1)&&hasDoDraw(2)&&hasDoDraw(3)){
                String fileName = "我和保单有个约会"+"_"+System.currentTimeMillis()+".xls";
                String filePath ="/usr/local/nginx/html/file/" + fileName;
                String fileHttpPath ="http://wx.picchealth.com/custom/file/" + fileName;

                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet("sheet1");

                Row row = sheet.createRow(0);

                Cell cell = row.createCell(0);
                cell.setCellValue("序号");
                cell = row.createCell(1);
                cell.setCellValue("奖项");
                cell = row.createCell(2);
                cell.setCellValue("姓名");
                cell = row.createCell(3);
                cell.setCellValue("手机号");


                int rowNum=1;
                for(PiccMeeting item : piccMeetingList){
                    row = sheet.createRow(rowNum);

                    cell = row.createCell(0);
                    cell.setCellValue(rowNum);
                    cell = row.createCell(1);
                    cell.setCellValue(rankNameMap.get(item.getDoPrizeStatus()-2));
                    cell = row.createCell(2);
                    cell.setCellValue(item.getUserName());
                    cell = row.createCell(3);
                    cell.setCellValue(item.getPhoneNum());
                    rowNum++;
                }
                FileOutputStream fos = new FileOutputStream(filePath);
                wb.write(fos);
                if(null != fos){
                    fos.close();
                }
                return fileHttpPath;
            }

        }catch (Exception e){

        }
        return "";
    }

    public void updateSameNameUser(String userName){
        Sql updateSql = Sqls.create("update piccMeeting set doPrizeStatus = -99 where userName = @userName and doPrizeStatus = 1");
        updateSql.setParam("userName",userName);
        this.execute(updateSql);
    }

    public void setDoDrawStatus(int rank){
        redisManager.set("piccMeeting:doDraw:"+rank,"1");
    }

    public boolean hasDoDraw(int rank){
        String ret = redisManager.get("piccMeeting:doDraw:"+rank);
        if(null == ret){
            return false;
        }
        return true;
    }

    public void resetDoPrizeStatus(){
        this.execute(Sqls.create("update piccMeeting set doPrizeStatus = 1"));
        redisManager.del("piccMeeting:doDraw:0");
        redisManager.del("piccMeeting:doDraw:1");
        redisManager.del("piccMeeting:doDraw:2");
        redisManager.del("piccMeeting:doDraw:3");
        redisManager.del("piccMeeting:doDraw:4");
        redisManager.del("piccMeeting:doDraw:5");
    }
}
